package com.glory.gloryquickui.widget.datepicker;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.glory.gloryquickui.R;
import com.glory.gloryquickui.entity.DateBean;

import java.util.List;


/**
 * 描述：
 * 时间：2018/1/27/027
 * 作者：xjh
 */
public class CustomMonthDateRangeAdapter extends RecyclerView.Adapter<CustomMonthDateRangeAdapter.Holder> {

    private Context context;
    private List<DateBean> mDateBeans;
    private int type = 0;//表示选中的类型 0表示开始时间 1表示结束时间
    private List<DateBean.DayBean> mDayBeans;//存取原始数据
    private onDateSelectListener mDateBeansener;
    private CustomDayDateRangeAdapter adapter;
    private int startMonth = 0, startDay = 0;
    private int selectSpace;
    private int selectType = 0;
    private String mYear, mMonth, mDay;

    public CustomMonthDateRangeAdapter(Context context, List<DateBean> mDateBeans) {
        this.context = context;
        this.mDateBeans = mDateBeans;


    }


    public interface onDateSelectListener {
        void onSelect(String date, int type);
    }

    public void setListener(onDateSelectListener onDateSelectListener) {
        this.mDateBeansener = onDateSelectListener;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_dateselect_month, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(final Holder holder, int position) {
        RecyclerView mRecyclerView = holder.mRecyclerView;
        DateBean dateBean = mDateBeans.get(position);
        holder.tv_year_month.setText(dateBean.getYear() + "年 "
                + dateBean.getMonth() + "月");
        mRecyclerView.setLayoutManager(new GridLayoutManager(mRecyclerView.getContext(), 7));
        adapter = new CustomDayDateRangeAdapter(context, dateBean.getDayBeans());
        mRecyclerView.setAdapter(adapter);
        mRecyclerView.setHasFixedSize(true);
        adapter.setListener(new CustomDayDateRangeAdapter.onDateSelectListener() {
            @Override
            public void onSelect(int pos) {
                if (mDateBeansener != null) {
                    handData(pos, holder);
                }
            }
        });
    }

    /**
     * 处理数据
     *
     * @param pos
     * @param holder
     */
    private void handData(int pos, Holder holder) {
        //  判读是否是第一次点击
        if (type == 0) {  //第一次点击
            firstSelect(pos, holder);
        } else {//第二次点击
            //判断第二次点击时间在上次点击之前还是之后
            if (startMonth == holder.getAdapterPosition()) {
                //同月的时候 比较天
                if (pos == startDay) {
                    //两次数据相等的时候 改变数据状态
                    mDateBeans.get(holder.getAdapterPosition()).getDayBeans().get(pos).setSelect(true);
                    type = 0;//改变点击状态
                } else if (pos > startDay) {
                    //之前 改变数据
                    selectSpace = 0;
                    mDateBeans.get(holder.getAdapterPosition()).getDayBeans().get(pos).setSelect(true);
                    for (int i = 0; i < mDateBeans.size(); i++) {
                        for (int i1 = 0; i1 < mDateBeans.get(i).getDayBeans().size(); i1++) {
                            if (mDateBeans.get(i).getDayBeans().get(i1).isSelect()) {
                                if (selectSpace == 0) {
                                    selectSpace = 1;
                                } else {
                                    selectSpace = 0;
                                }
                            }
                            if (selectSpace == 1) {
                                mDateBeans.get(i).getDayBeans().get(i1).setMiddle(true);
                            }
                        }
                    }
                    type = 0;//改变点击状态
                } else {
                    //之后 按第一点击处理
                    firstSelect(pos, holder);
                }
            } else if (startMonth < holder.getAdapterPosition()) {
                //之前 处理数据
                mDateBeans.get(holder.getAdapterPosition()).getDayBeans().get(pos).setSelect(true);
                for (int i = startMonth; i <= holder.getAdapterPosition(); i++) {
                    if (holder.getAdapterPosition()== i) {
                        for (int i1 = 0; i1 < pos; i1++) {
                            mDateBeans.get(i).getDayBeans().get(i1).setMiddle(true);
                        }
                    } else if (startMonth == i) {
                        for (int i1 = startDay; i1 < mDateBeans.get(i).getDayBeans().size(); i1++) {
                            mDateBeans.get(i).getDayBeans().get(i1).setMiddle(true);
                        }
                    } else {
                        for (int i1 = 0; i1 < mDateBeans.get(i).getDayBeans().size(); i1++) {
                            mDateBeans.get(i).getDayBeans().get(i1).setMiddle(true);
                        }
                    }
                }
                type = 0;//改变点击状态
            } else {
                //之后 则设按第一次点击处理
                firstSelect(pos, holder);
            }
        }
        if (mDateBeans.get(holder.getAdapterPosition()).getMonth() < 10) {
            mMonth = "0" + mDateBeans.get(holder.getAdapterPosition()).getMonth();
        } else {
            mMonth = mDateBeans.get(holder.getAdapterPosition()).getMonth() + "";
        }
        if (mDateBeans.get(holder.getAdapterPosition()).getDayBeans().get(pos).getDay() < 10) {
            mDay = "0" + mDateBeans.get(holder.getAdapterPosition()).getDayBeans().get(pos).getDay();
        } else {
            mDay = mDateBeans.get(holder.getAdapterPosition()).getDayBeans().get(pos).getDay() + "";
        }
        mYear = mDateBeans.get(holder.getAdapterPosition()).getYear() + "";
        String date = mYear + "-" + mMonth + "-" + mDay;

        DateSelectChanage();
        mDateBeansener.onSelect(date, type);

    }

    /**
     * 第一次选择
     *
     * @param pos
     * @param holder
     */
    private void firstSelect(int pos, Holder holder) {
        startMonth = holder.getAdapterPosition();  //存入点击的月份的下标
        startDay = pos;  //存入点击的日期的下标
        //将数据归零
        for (int i = 0; i < mDateBeans.size(); i++) {
            for (int i1 = 0; i1 < mDateBeans.get(i).getDayBeans().size(); i1++) {
                mDateBeans.get(i).getDayBeans().get(i1).setSelect(false);
                mDateBeans.get(i).getDayBeans().get(i1).setMiddle(false);
            }
        }
        //改变数据
        mDateBeans.get(holder.getAdapterPosition()).getDayBeans().get(pos).setSelect(true);
        type = 1; //改变点击状态

    }

    public void DateSelectChanage() {
        adapter.notifyDataSetChanged();
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mDateBeans.size();
    }

    class Holder extends RecyclerView.ViewHolder {
        TextView tv_year_month;
        RecyclerView mRecyclerView;

        Holder(View itemView) {
            super(itemView);
            tv_year_month =  itemView.findViewById(R.id.tv_year_month);
            mRecyclerView = itemView.findViewById(R.id.rvDate);
        }
    }

}
